<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Auths;
use app\admin\model\Builder;
use app\admin\model\Qinu;
use think\Controller;
use think\Db;
use think\Request;

class Lianbiao extends Controller
{
    public function show_list(Request $request){
        $modelid = $request->param('modelid',null);
        $tiaojian = $request->param('tiaojian',null);
        $this->assign('tiaojian',$tiaojian);
        $this->assign('modelid',$modelid);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $model   = Db::name('model')->where('id',$modelid)->find();
        $page    = $request->param('page',null);
        $limit   = $request->param('limit',10);
        $pid = null;
        if($tiaojian){
            $pid = Db::name($model['surface'])->where('name',$tiaojian)->find();
//            return json($pid);
        }
        if(!$page){
            if($tiaojian){
                if($pid){
                    $data = Db::name($model['surface'])->where('pid',$pid['id'])->whereOr('name',$tiaojian)->order('pid asc')->paginate($limit)->toArray();
                }else{
                    $data = Db::name($model['surface'])->where('name','like',"%$tiaojian%")->order('pid asc')->paginate($limit)->toArray();
                }
            }else{
                $data = Db::name($model['surface'])->order('pid asc')->paginate($limit)->toArray();
            }

            $this->assign('data',$data['total']);
            $data = $data['data'];
        }else{
            $data = Db::name($model['surface'])->order('pid asc')->limit(($page-1)*$limit,$limit)->select();
        }

        $this->assign('modelid',$modelid);
        foreach ($data as $key=>$value){
            if($value['pid']==0){
                $data[$key]['pid_name'] = '最上级';
            }else{
                $data[$key]['pid_name'] = (Db::name($model['surface'])->where('id',$value['pid'])->find())['name'];
            }
            $data[$key]['create_time'] = date('Y-m-d',$value['create_time']);
        }
        if($page){
            return json(['code'=>200,'data'=>$data]);
        }
        $str = Builder::make('table','','','','','')
            ->addHead(['button alldel','button add'],['批量删除','添加'],['/admin/lianbiao/del?modelid='.$modelid,'/admin/lianbiao/show_add?model_id='.$modelid],['',''],['',''])
            ->addColumn('INDEX','checkbox','<input type="checkbox"/>','','text-align: center')
            ->addColumn('id','text','ID','','text-align: center')
            ->addColumn('name','text','名称','','text-align: center')
            ->addColumn('pid_name','text','上级','','text-align: center')
            ->addColumn('sort','text','排序','','text-align: center')
            ->addColumn('tubiao','img','图标','','text-align: center')
            ->addColumn('create_time','text','创建时间','','text-align: center')
            ->addRight('操作',['icon edit','icon del'],['/admin/lianbiao/show_edit?modelid='.$modelid.'&id=','/admin/lianbiao/del?modelid='.$modelid],['编辑','删除'],'','text-align: center')
            ->addData($data)
//        echo '<pre>';print_r($str);exit;
            ->addPage('test')
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('list');
    }
    public function show_add(Request $request){
        $modelid = $request->param('model_id',null);
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
//        return $modelid;
        $model = Db::name('model')->where('id',$modelid)->find();
        $pid   = Db::name($model['surface'])->select();
        $option[0] = '最上级';
        foreach ($pid as $value){
            $option[$value['id']] = $value['name'];
        }
//        echo '<pre>';print_r($feild);exit;
        $str = Builder::make('form','/admin/lianbiao/add?modelid='.$modelid,'','')
            ->addInput('name','名称','')
            ->addSelect('pid','上级','',$option)
            ->addInput('sort','排序')
            ->addImg('tubiao','图标')
            ->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }

    /**添加联表
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request){
        $data = $request->post();
        $modelid = $request->param('modelid');
        $model = Db::name('model')->where('id',$modelid)->find();
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        if(!$data['sort']){
            $data['sort'] = 100;
        }
        $data['create_time'] = time();
        if(Db::name($model['surface'])->insert($data)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }else{
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }
    }
    public function show_edit(Request $request){
        $modelid = $request->param('modelid');
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $id = $request->param('id');
        $model = Db::name('model')->where('id',$modelid)->find();
        $data = Db::name($model['surface'])->where('id',$id)->find();
        $pid   = Db::name($model['surface'])->select();
        $option[0] = '最上级';
        foreach ($pid as $value){
            if(!($value['id']==$id)){
                $option[$value['id']] = $value['name'];
            }
        }
//        return 1;
        $str = Builder::make('form','/admin/lianbiao/update?modelid='.$modelid,'','')
            ->addNone('id',$id)
        ->addInput('name','名称',$data['name'])
            ->addSelect('pid','上级',$data['pid'],$option)
            ->addInput('sort','排序',$data['sort'])
            ->addImg('tubiao','图标',$data['tubiao'])
            ->addButton(['提交','返回'],['submit','back'])
            ->fetch();
        $this->assign('str',$str);
        return $this->fetch('public/bianji');
    }
    public function update(Request $request){
        $modelid= $request->param('modelid');
        if(!Auths::login_auth($modelid)){
            return '你没有权限';
        }
        $data = $request->post();
        $model = Db::name('model')->where('id',$modelid)->find();
        if(Db::name($model['surface'])->where('id',$data['id'])->update($data)){
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }else{
            return $this->fetch('public/error',['code'=>200,'action'=>'/admin/lianbiao/show_list?modelid='.$modelid]);
        }
    }
    public function del(Request $request){
        $modelid = $request->param('modelid');
        if(!Auths::login_auth($modelid)){
            return json(['code'=>203,'msg'=>'你没有权限']);
        }
        $id      = $request->param('id');
        $model = Db::name('model')->where('id',$modelid)->find();
        $lao  = Db::name($model['surface'])->where('id','in',$id)->find();
        if($lao['tubiao']){
            Qinu::DeleFileForId($lao['tubiao']);
        }
        if(Db::name($model['surface'])->where('id','in',$id)->delete()){
                return json(['code'=>200,'msg'=>'删除成功']);
        }else{
                return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**联表分页
     * @param Request $request
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function pages(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $tiaojian = $request->param('tiaojian',null);
        $modelid = $request->param('modelid',null);
        $data = [];
        if(!$modelid){
            return $data;
        }
        $model = Db::name('model')->where('id',$modelid)->find();
        if($tiaojian){
            $pid = Db::name($model['surface'])->where('name',$tiaojian)->find();
//            return json($pid);
        }
//        return $model;
        if($tiaojian){
            if($pid){
                $data = Db::name($model['surface'])->where('pid',$pid['id'])->whereOr('name',$tiaojian)->order('pid asc')->limit(($page-1)*$limit,$limit)->select();
            }else{
                $data = Db::name($model['surface'])->where('name','like',"%$tiaojian%")->order('pid asc')->limit(($page-1)*$limit,$limit)->select();
            }
        }else{
            $data = Db::name($model['surface'])->order('pid asc')->limit(($page-1)*$limit,$limit)->select();
        }

        foreach ($data as $key=>$value){
            if($value['pid']==0){
                $data[$key]['pid_name'] = '最上级';
            }else{
                $data[$key]['pid_name'] = (Db::name($model['surface'])->where('id',$value['pid'])->find())['name'];
            }
            $data[$key]['create_time'] = date('Y-m-d',$value['create_time']);
            if($value['tubiao']){
                $data[$key]['tubiao'] = getFilePath($value['tubiao']);
            }
        }
        return json($data);
    }
}